package cn.dongqihong.hehe;

import java.awt.*;
import java.io.File;
import java.net.URLDecoder;

/**
 * ttf字体文件
 *
 * @author DongQihong
 */
public class ImgFontByte {
    private static ImgFontByte imgFontByte = new ImgFontByte();
    private String path = null;
    private File fontFile = null;

    private ImgFontByte() {
        path = ImgFontByte.class.getResource("/simhei.ttf").getFile();
        try {
            path = URLDecoder.decode(path, "utf-8");
        } catch (Exception e) {
            e.getMessage();
        }
        fontFile = new File(path);
    }

    public static ImgFontByte getInstance() {
        return imgFontByte;
    }

    /**
     * 获取字体
     *
     * @param fontHeight 字体高
     * @return 字体
     */
    public Font getFont(int fontHeight) {
        try {
            Font baseFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
            return baseFont.deriveFont(Font.PLAIN, fontHeight);
        } catch (Exception e) {
            return new Font("Arial", Font.PLAIN, fontHeight);
        }
    }
}
